<html>
<body>
Type an expression in the following box (e.g., 10.5+20*3/25).<br><br> 

<form action="calculator.php" method="get" >
 <input type="text" name="exp" />
 <input type="submit" value="Calculate" />
</form>

<?php

 
 ini_set("display_errors", 0);
 //ini_set("log_errors", 1);

 //ini_set("error_log", "errlog");

 $exp= $_GET["exp"];

 //input checking
 if(preg_match("#[^0-9+-/*\s]#", $exp))      //handle invalid characters
   echo "invalid input expression: " . $exp;
 elseif(preg_match("#[+/*.][[:space:]]*[+/*.]+#", $exp)) //multi ops
   echo "invalid input expression: " . $exp;
 elseif(preg_match("#[+-/*.]--+#", $exp))    //op followed by - -
   echo "invalid input expression: " . $exp;
 elseif(preg_match("#--#", $exp))
   echo "invalid input expression: " . $exp;
 elseif(preg_match("#[+-/*]$|^[+/*]#", $exp)) // leading/trailing ops
   echo "invalid input expression: " . $exp;
 elseif(preg_match("#/0[^.]|/0\.0+[^0-9]#", $exp))   //div by 0
   echo "invalid input expression (division by zero): " . $exp;
 elseif(preg_match("#[.][[0-9]*[.]#", $exp))
  echo "invalid input expression: " . $exp;
 elseif($exp == "")
   ;
 else {	
   echo $exp . " = ";
   eval("echo $exp;");
 }
?>

</html>
</body>
